/**
 *  Copyright [2011] Steffen Kämpke
 *  mailto: steffen.kaempke@t-online.de
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pennychecker.swing.presenter.ext.component;

import com.pennychecker.swing.presenter.ext.event.ChangeHandler;
import com.pennychecker.swing.presenter.ext.event.FocusEvent;
import com.pennychecker.swing.presenter.ext.event.FocusHandler;
import com.pennychecker.swing.presenter.ext.event.HasFocusHandlers;
import com.pennychecker.swing.presenter.ext.event.HasKeyPressHandlers;
import com.pennychecker.swing.presenter.ext.event.KeyPressEvent;
import com.pennychecker.swing.presenter.ext.event.KeyPressHandler;
import com.pennychecker.swing.presenter.ext.listener.SwingFocusListenerAdapter;
import com.pennychecker.swing.presenter.ext.listener.SwingKeyAdapter;
import com.pennychecker.swing.mvp.event.HandlerRegistration;
import com.pennychecker.swing.mvp.event.MvpEvent;
import com.pennychecker.swing.presenter.ext.event.ChangeEvent;
import com.pennychecker.swing.presenter.ext.event.HasChangeHandlers;
import com.pennychecker.swing.presenter.ext.listener.DocumentListenerAdapter;
import java.awt.Color;
import java.awt.event.FocusAdapter;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.text.Document;

/**
 *
 * @author Steffen Kämpke mailto:steffen.kaempke@t-online.de
 */
public final class STextField extends JTextField implements HasKeyPressHandlers, HasFocusHandlers, HasChangeHandlers {

    private final HandlerAdapter componentAdapter = new HandlerAdapter();


    @Override
    public void fireEvent(MvpEvent<?> event) {
        this.componentAdapter.fireEvent(event);
    }
    private int minChar = 0;
    private int maxChar = 255;
    private Color errorBackGround = new Color(255, 205, 205);
    private Color validBackGround = new Color(225, 255, 204);
    private Color defaultBackground = new Color(255, 255, 255);
    private boolean dateField = false;
    private boolean currencyField = false;
    private NumberFormat numberFormat = NumberFormat.getNumberInstance(getLocale());
    public final SimpleDateFormat germanDateFormat = new SimpleDateFormat("dd.MM.yyyy");
    public final SimpleDateFormat usDateFormat = new SimpleDateFormat("yyyy/MM/dd");
    private boolean error = false;
    private boolean emptyAllowed = false;

    public void setAsCurrencyField(boolean currencyField) {
        this.currencyField = currencyField;
    }

    public void setAsDateField(boolean dateField) {
        this.dateField = dateField;
    }

    public void setMinChar(int min, boolean emptyAllowed) {
        assert min >= 0;
        this.minChar = min;
        this.emptyAllowed = emptyAllowed;
    }

    public void setMaxChar(int max) {

        assert max >= 0;
        this.maxChar = max;
    }

    public void setErrorBackground(Color color) {
        assert null != color;
        errorBackGround = color;
    }

    public void setValidBackground(Color color) {
        assert null != color;
        validBackGround = color;
    }

    public void setDefaultBackground(Color color) {
        assert null != color;
        defaultBackground = color;
    }

    public void setError() {
        this.setBackground(errorBackGround);
        error = true;
    }

    public void setValid() {
        this.setBackground(validBackGround);
        error = false;
    }

    public void setDeafault() {
        this.setBackground(defaultBackground);
        error = false;
    }

    public boolean hasError() {
        return error;
    }

    public void checkInput() {
    }
    private String errorLabel;

    public void setErrorLabel(String name) {
        errorLabel = name;
    }

    public String getErrorLabel() {
        return errorLabel;
    }
    private String infoLabel;

    public void setInfoLabel(String name) {
        infoLabel = name;
    }

    public String getInfoLabel() {
        return infoLabel;
    }

    @Override
    public HandlerRegistration addFocusHandler(FocusHandler handler) {
        super.addFocusListener(new SwingFocusListenerAdapter(componentAdapter));
        return componentAdapter.addHandler(handler, FocusEvent.TYPE);
    }

    @Override
    public HandlerRegistration addKeyPressHandler(KeyPressHandler handler) {
        super.addKeyListener(new SwingKeyAdapter(componentAdapter));
        return componentAdapter.addHandler(handler, KeyPressEvent.TYPE);
    }

    @Override
    public HandlerRegistration addChangeHandler(ChangeHandler handler) {
        super.getDocument().addDocumentListener(new DocumentListenerAdapter(componentAdapter));
        return componentAdapter.addHandler(handler, ChangeEvent.TYPE);
    }
    
    
    
}
